package com.atom.wyz.new_opengl.simple;

import android.content.Context;
import android.content.res.Resources;
import android.opengl.GLES30;
import android.view.MotionEvent;

import com.atom.wyz.new_opengl.AbstractGLSurfaceView;
import com.atom.wyz.new_opengl.R;
import com.atom.wyz.new_opengl.base.ObjectVNT;
import com.atom.wyz.new_opengl.base.TextureRect;
import com.atom.wyz.new_opengl.util.LoadUtil;
import com.atom.wyz.new_opengl.util.MatrixState;
import com.atom.wyz.new_opengl.util.TextureUtil;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class SimpleGLSurfaceView extends AbstractGLSurfaceView {
    private final float TOUCH_SCALE_FACTOR = 180.0f/320;//角度缩放比例
    private float mPreviousY;//上次的触控位置Y坐标
    private float mPreviousX;//上次的触控位置X坐标
    //摄像机位置相关
    float cx=0;
    float cy=30;
    float cz=60;
    float cAngle=0;
    final float cR=60;
    //灯光位置
    float lx=60;
    float ly=80;
    float lz=60;
    float lAngle=0;
    final float lR=1;
    //灯光投影Up向量
    float ux=0;
    float uy=0;
    float uz=1;
    public SimpleGLSurfaceView(Context context) {
        super(context);
        new Thread()
        {
            public void run()
            {
                while(true)
                {
                    lAngle += 0.5;//改变up向量绕Y轴的旋转角度
                    ux=(float) Math.sin(Math.toRadians(lAngle))*lR;//根据角度计算当前up向量的X分量
                    uz=(float) Math.cos(Math.toRadians(lAngle))*lR;//根据角度计算当前up向量的Z分量
                    try {
                        Thread.sleep(20);//线程休眠
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    //触摸事件回调方法
    @Override
    public boolean onTouchEvent(MotionEvent e)
    {
        float y = e.getY();
        float x = e.getX();
        switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float dy = y - mPreviousY;//计算触控笔Y位移
                float dx = x - mPreviousX;//计算触控笔X位移
                cAngle += dx * TOUCH_SCALE_FACTOR;//设置沿x轴旋转角度

                cx=(float) Math.sin(Math.toRadians(cAngle))*cR;
                cz=(float) Math.cos(Math.toRadians(cAngle))*cR;

                cy+= dy/10.0f;//设置沿z轴移动
                requestRender();//重绘画面
        }
        mPreviousY = y;//记录触控笔位置
        mPreviousX = x;//记录触控笔位置
        return true;
    }

    @Override
    protected Renderer getRenderer() {
        return new SimpleRenderer();
    }

    class SimpleRenderer extends AbstractRenderer{
        TextureRect textureRect ;
        ObjectVNT cft ;
        ObjectVNT ch ;
        ObjectVNT pm ;
        ObjectVNT qt ;
        ObjectVNT yh ;
        int textureId ;
        @Override
        protected void create(GL10 gl10, EGLConfig eglConfig) {
            textureRect = new TextureRect(activity.getResources() , 0.2f , 10f , 10f ) ;
            cft = getObj("obj/cft.obj") ;
            cft = getObj("obj/ch.obj") ;
            cft = getObj("obj/pm.obj") ;
            cft = getObj("obj/qt.obj") ;
            cft = getObj("obj/yh.obj") ;

            MatrixState.setInitMatrix() ;
            textureId = TextureUtil.initTextureDefault(R.drawable.texture , getResources()) ;
        }
        private ObjectVNT getObj(String path){
            final ObjectVNT[] temp = new ObjectVNT[1];
            LoadUtil.getLoadObj(getResources(), path , new LoadUtil.CreateObjectLoader() {
                @Override
                public void create(Resources resources, float[] vertexArray, float[] normalArray, float[] texcoorsArray) {
                    temp[0] =   new ObjectVNT(resources , vertexArray , normalArray , texcoorsArray) ;
                }
            });
            return temp[0];
        }
        @Override
        protected void changed(GL10 gl10, int i, int i1, float ratio) {
//            MatrixState.setCamera(0, 0, 2, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
//            MatrixState.setProjectFrustum(-ratio , ratio  ,-1 , 1 , 1 , 5);
        }

        @Override
        protected void draw(GL10 gl10) {
            MatrixState.setCamera(cx,cy,cz,0f,0f,0f,0f,1f,0f);
            MatrixState.setLightLocation(lx,ly,lz);
            MatrixState.setProjectFrustum(-ratio, ratio, -0.5f, 0.5f, 1f, 400);
            cft.drawSelf(textureId);

        }
    }
}
